﻿using System.Collections.Generic;
using System.Reflection;

namespace Lightray.Hattrick.ChppLib
{
    public class ComDelegate<T>
    {
        private object _target;

        public ComDelegate(object target)
        {
            _target = target;
        }

        public T Invoke()
        {
            return Invoke(null);
        }

        public T Invoke(params object[] parameters)
        {
            return invokeComCall(parameters);
        }

        private T invokeComCall(object[] parameters)
        {
            List<object> listParameters = new List<object>();
            listParameters.Add(_target);
            if (parameters != null)
                listParameters.AddRange(parameters);

            object result = _target.GetType().InvokeMember("call",
                                                           BindingFlags.InvokeMethod,
                                                           null,
                                                           _target,
                                                           listParameters.ToArray());
            return (T)result;
        }
    };

    public class ComDelegate
    {
        private ComDelegate<object> _target;

        public ComDelegate(object target)
        {
            _target = new ComDelegate<object>(target);
        }

        public void Invoke()
        {
            _target.Invoke();
        }

        public void Invoke(params object[] parameters)
        {
            _target.Invoke(parameters);
        }
    }; 
}         